﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using ComLib.ComFun;
using ComLib.ConfigService;

namespace KAImage.Config
{
    /// <summary>
    /// 通道色相饱和度选项控制
    /// </summary>
    public class ChannelHSLOffset
    {
        /// <summary>
        /// 色相偏移[-180, 180]
        /// </summary>
        private int _hue = 0;
        public int Hue
        {
            get { return _hue; }
            set { _hue = value; }
        }

        /// <summary>
        /// 饱和度偏移[-100, 100]
        /// </summary>
        private int _saturation = 0;
        public int Saturation
        {
            get { return _saturation; }
            set { _saturation = value; }
        }

        /// <summary>
        /// 明度(亮度)偏移[-100, 100]
        /// </summary>
        private int _light = 0;
        public int Lightness
        {
            get { return _light; }
            set { _light = value; }
        }

        public ChannelHSLOffset()
        {

        }

        public ChannelHSLOffset(int h, int s, int b)
        {
            _hue = h;
            _saturation = s;
            _light = b;
        }

        public void SetOffsets(int h, int s, int b)
        {
            _hue = h;
            _saturation = s;
            _light = b;
        }

        public void SetOffsets(ChannelHSLOffset opt)
        {
            _hue = opt._hue;
            _saturation = opt._saturation;
            _light = opt._light;
        }


        public void Add(ChannelHSLOffset opt)
        {
            _hue += opt._hue;
            _saturation += opt._saturation;
            _light += opt._light;
        }

        public void Reset()
        {
            _hue = 0;
            _saturation = 0;
            _light = 0;
        }

        [XmlIgnore]
        public bool IsDefault
        {
            get
            {
                if (_hue == 0 && _saturation == 0 && _light == 0)
                {
                    return true;
                }
                return false;
            }
        }

        public override string ToString()
        {
            StringBuilder builder = new StringBuilder("通道HSL调整选项: ");
            FillFieldValueStringDelegate FillString =
               (string field, object val) =>
               {
                   if (val != null)
                   {
                       builder.Append("[");
                       builder.Append(field);
                       builder.Append("]: ");
                       builder.Append(val.ToString() + "; ");
                   }
               };
            FillString("[色相偏移]:", _hue);
            FillString("[饱和度偏移]:", _saturation);
            FillString("[亮度]:", _light);
          
            return builder.ToString();
        }
    }

    /// <summary>
    /// 图像色相/饱和度/亮度(HSL)调整配置选项
    /// </summary>
    [Serializable]
    [XmlRootAttribute("ImageHSLOffset", Namespace = "KAImage.Config", IsNullable = false)]
    public class ImageHSLOffset : XmlSerializeBase
    {
      
        /// <summary>
        /// 所有选项集合
        /// </summary>
        private List<ChannelHSLOffset> _offsets = new List<ChannelHSLOffset>();

        /// <summary>
        /// 全图
        /// </summary>
        public ChannelHSLOffset General 
        {
            get { return _offsets[0]; }
            set { _offsets[0] = value; } 
        }

        /// <summary>
        /// 红
        /// </summary>
        public ChannelHSLOffset Red
        {
            get { return _offsets[1]; }
            set { _offsets[1] = value; } 
        }

        /// <summary>
        /// 黄
        /// </summary>
        public ChannelHSLOffset Yellow 
        {
            get { return _offsets[2]; }
            set { _offsets[2] = value; } 
        }

        /// <summary>
        /// 绿
        /// </summary>
        public ChannelHSLOffset Green
        {
            get { return _offsets[3]; }
            set { _offsets[3] = value; } 
        }

        /// <summary>
        /// 青
        /// </summary>
        public ChannelHSLOffset Cyan
        {
            get { return _offsets[4]; }
            set { _offsets[4] = value; } 
        }

        /// <summary>
        /// 蓝
        /// </summary>
        public ChannelHSLOffset Blue 
        {
            get { return _offsets[5]; }
            set { _offsets[5] = value; } 
        }

        /// <summary>
        /// 洋红
        /// </summary>
        public ChannelHSLOffset Magenta
        {
            get { return _offsets[6]; }
            set { _offsets[6] = value; } 
        }

        /// <summary>
        /// 检查色相是否需要更新
        /// </summary>
        [XmlIgnore]
        public bool IsDefaultHue
        {
            get
            {
                return (_offsets.FindIndex(r => r.Hue != 0) < 0);
            }
        }

        /// <summary>
        /// 检查全图明度是不是默认值
        /// </summary>
        [XmlIgnore]
        public bool IsDefaultGeneralLightness
        {
            get
            {
                return General.Lightness == 0;
            }
        }

        /// <summary>
        /// 检查单通道的明度是不是默认值
        /// </summary>
        [XmlIgnore]
        public bool IsDefaultLightness
        {
            get
            {
                return (_offsets.FindIndex(1, r => r.Lightness != 0) < 0);
            }
        }

        /// <summary>
        /// 检查饱和度是不是默认值
        /// </summary>
        [XmlIgnore]
        public bool IsDefaultSaturation
        {
            get
            {
                return (_offsets.FindIndex(r => r.Saturation != 0) < 0);
            }
        }

        /// <summary>
        /// 检查所有选项是不是默认值
        /// </summary>
        [XmlIgnore]
        public bool IsDefault
        {
            get
            {
                return (_offsets.FindIndex(r => !r.IsDefault) < 0);
            }
        }

        private int _selectedIndex = 0;
        [XmlIgnore]
        public int SelectedIndex
        {
            get { return _selectedIndex; }
            set { _selectedIndex = value; }
        }

        [XmlIgnore]
        public ChannelHSLOffset SelectedOption
        {
            get
            {
                if (_selectedIndex < 0 || _selectedIndex >= _offsets.Count)
                {
                    throw new Exception("指定的索引无效.");
                }
                return _offsets[_selectedIndex];
            }
        }

        public ImageHSLOffset()
        {
            InitOptions();
        }

        public void InitOptions()
        {
            _offsets.Clear();
            for (int i = 0; i < 7; i++)
            {
                _offsets.Add(new ChannelHSLOffset());
            }
            General = _offsets[0];
            Red = _offsets[1];
            Yellow = _offsets[2];
            Green = _offsets[3];
            Cyan = _offsets[4];
            Blue = _offsets[5];
            Magenta = _offsets[6];
        }

        public override bool Serialize()
        {
            return Serializer.SaveToXml(ConfigFilePath, this);
        }

        public override bool Load()
        {
            ImageHSLOffset obj = (ImageHSLOffset)Serializer.LoadFromXml(ConfigFilePath, this.GetType());
            if (obj != null)
            {
                this.General = obj.General;
                this.Red = obj.Red;
                this.Yellow = obj.Yellow;
                this.Green = obj.Green;
                this.Cyan = obj.Cyan;
                this.Blue = obj.Blue;
                this.Magenta = obj.Magenta;
                this._nVer = obj._nVer;
                return true;
            }
            else
            {
                ErrorMessage = Serializer.Message;
                return false;
            }
        }

        public override string ToString()
        {
            StringBuilder builder = new StringBuilder("图像HSL调整选项: ");
            FillFieldValueStringDelegate FillString =
               (string field, object val) =>
               {
                   if (val != null)
                   {
                       builder.Append("[");
                       builder.Append(field);
                       builder.Append("]: ");
                       builder.Append(val.ToString() + "; ");
                   }
               };
            FillString("[General]:", General);
            FillString("[Red]:", Red);
            FillString("[Yellow]:", Yellow);
            FillString("[Green]:", Green);
            FillString("[Cyan]:", Cyan);
            FillString("[Blue]:", Blue);
            FillString("[Magenta]:", Magenta);
            return builder.ToString();
        }
    }
}
